package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.RocketHJD82;

public class RocketHJD82Sql {

	private static RocketHJD82Sql	instance						= null;
	private final String			TABLE_NAME						= "RocketHJD82";

	private final String			KEY_ROCKETHJD82_ID				= "id";
	private final String			KEY_ROCKETHJD82_ANGEL			= "angel";			//角度
	private final String			KEY_ROCKETHJD82_START_LENGTH	= "startlength";
	private final String			KEY_ROCKETHJD82_START_HEIGHT	= "startheight";
	private final String			KEY_ROCKETHJD82_END_LENGTH		= "endlength";
	private final String			KEY_ROCKETHJD82_END_HEIGHT		= "endheight";
	private final String			KEY_ROCKETHJD82_TOP_LENGTH		= "toplength";
	private final String			KEY_ROCKETHJD82_TOP_HEIGHT		= "topheight";
	private final String			KEY_ROCKETHJD82_LENGTH			= "length";

	public static synchronized RocketHJD82Sql getInstance() {
		if (null == instance) {
			instance = new RocketHJD82Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_ROCKETHJD82_ID + " INTEGER PRIMARY KEY," + KEY_ROCKETHJD82_ANGEL + " INTEGER,"
					+ KEY_ROCKETHJD82_START_LENGTH + " INTEGER," + KEY_ROCKETHJD82_START_HEIGHT + " INTEGER,"
					+ KEY_ROCKETHJD82_END_LENGTH + " INTEGER," + KEY_ROCKETHJD82_END_HEIGHT + " INTEGER,"
					+ KEY_ROCKETHJD82_TOP_LENGTH + " INTEGER," + KEY_ROCKETHJD82_TOP_HEIGHT + " INTEGER," + KEY_ROCKETHJD82_LENGTH + "  INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(RocketHJD82 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETHJD82_ANGEL, rocket.angle);
			values.put(KEY_ROCKETHJD82_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETHJD82_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETHJD82_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETHJD82_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETHJD82_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETHJD82_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETHJD82_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(RocketHJD82 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETHJD82_ANGEL, rocket.angle);
			values.put(KEY_ROCKETHJD82_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETHJD82_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETHJD82_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETHJD82_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETHJD82_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETHJD82_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETHJD82_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_ROCKETHJD82_ID + " = ?", new String[] { String.valueOf(rocket.id) });
		}
		return 0;
	}

	public void delete(RocketHJD82 rocket) {
		delete(rocket.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_ROCKETHJD82_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<RocketHJD82> query() {
		ArrayList<RocketHJD82> rockets = new ArrayList<RocketHJD82>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_ROCKETHJD82_TOP_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						RocketHJD82 rocket = new RocketHJD82();
						rocket.id = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_ID));
						rocket.angle = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_ANGEL));
						rocket.startLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_START_LENGTH));
						rocket.startHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_START_HEIGHT));
						rocket.endLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_END_LENGTH));
						rocket.endHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_END_HEIGHT));
						rocket.topLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_TOP_LENGTH));
						rocket.topHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_TOP_HEIGHT));
						rocket.length = cur.getInt(cur.getColumnIndex(KEY_ROCKETHJD82_LENGTH));
						rockets.add(rocket);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return rockets;
	}

	private void initData() {
		RocketHJD82 rocket = new RocketHJD82();

		rocket.angle = 85;
		rocket.startHeight = 4000;
		rocket.startLength = 400;
		rocket.endHeight = 7800;
		rocket.endLength = 1600;
		rocket.topHeight = 8200;
		rocket.topLength = 1200;
		rocket.length = 2500;
		insert(rocket);

		rocket.angle = 80;
		rocket.startHeight = 3900;
		rocket.startLength = 800;
		rocket.endHeight = 7400;
		rocket.endLength = 2500;
		rocket.topHeight = 7900;
		rocket.topLength = 2000;
		rocket.length = 3435;
		insert(rocket);

		rocket.angle = 75;
		rocket.startHeight = 3700;
		rocket.startLength = 1000;
		rocket.endHeight = 6800;
		rocket.endLength = 3300;
		rocket.topHeight = 7500;
		rocket.topLength = 2600;
		rocket.length = 4370;
		insert(rocket);

		rocket.angle = 70;
		rocket.startHeight = 3600;
		rocket.startLength = 1300;
		rocket.endHeight = 6200;
		rocket.endLength = 4100;
		rocket.topHeight = 7100;
		rocket.topLength = 3400;
		rocket.length = 5305;
		insert(rocket);

		rocket.angle = 65;
		rocket.startHeight = 3400;
		rocket.startLength = 1500;
		rocket.endHeight = 5800;
		rocket.endLength = 4800;
		rocket.topHeight = 6600;
		rocket.topLength = 4000;
		rocket.length = 6240;
		insert(rocket);

		rocket.angle = 60;
		rocket.startHeight = 3100;
		rocket.startLength = 1900;
		rocket.endHeight = 5100;
		rocket.endLength = 5600;
		rocket.topHeight = 6000;
		rocket.topLength = 4600;
		rocket.length = 7175;
		insert(rocket);

		rocket.angle = 55;
		rocket.startHeight = 2900;
		rocket.startLength = 2000;
		rocket.endHeight = 4600;
		rocket.endLength = 6300;
		rocket.topHeight = 5600;
		rocket.topLength = 5100;
		rocket.length = 8110;
		insert(rocket);

		rocket.angle = 50;
		rocket.startHeight = 2500;
		rocket.startLength = 2200;
		rocket.endHeight = 3300;
		rocket.endLength = 7000;
		rocket.topHeight = 4900;
		rocket.topLength = 5600;
		rocket.length = 9045;
		insert(rocket);

		rocket.angle = 45;
		rocket.startHeight = 2200;
		rocket.startLength = 2400;
		rocket.endHeight = 3100;
		rocket.endLength = 7800;
		rocket.topHeight = 4200;
		rocket.topLength = 6000;
		rocket.length = 9980;
		insert(rocket);

	}
}
